//
// Created by 周智超 on 2020/6/21.
//

#ifndef STRUCTURE_ARRAY_H
#define STRUCTURE_ARRAY_H
#include "normal.h"
#include <iostream>
#include "sqlist.h"

void trsmat(int A[][maxSize], int B[][maxSize], int m, int n)
{
    for(int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; ++j)
        {
            B[j][i] = A[i][j];
        }
    }
}

void add(int C[][maxSize], int A[][maxSize], int B[][maxSize], int m, int n)
{
    for (int i = 0; i < m; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            C[i][j] = A[i][j] + B[i][j];
        }
    }
}

void mutmat(int C[][maxSize], int A[][maxSize], int B[][maxSize], int m, int n, int k)
{
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < k;j++)
        {
            C[i][j] = 0;
            for(int q = 0; q < n; q++)
            {
                C[i][j] += A[i][q] * B[q][j];
            }
        }
    }
}

void moveZero(int A[], int length)
{
    int i;
    int j = 0;
    for(i = 0; i < length; i++)
    {
        if(A[i] != 0)
        {
            A[j] = A[i];
            j++;
        }
    }
    for(; j < length;j++)
    {
        A[j] = 0;
    }
}


void printArr(int A[][maxSize], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; ++j)
        {
            std::cout << A[i][j] << " " ;
        }
        std::cout << std::endl;
    }
}


float recurMax(float a[], int length)
{
    float max, potential;
    if(length == 1)
    {
        return a[0];
    }
    max = a[0];
    potential = recurMax(a + 1, length -1);
    if(potential > max)
    {
        max = potential;
    }
    return max;
}

float recurSum(float a[], int length)
{
    if(length == 1)
    {
        return a[0];
    }
    return a[0] + recurSum(a + 1, length -1);

}

float recurAverage(float a[], int length)
{
    if(length == 1)
    {
        return a[0];
    }
    return (a[0] + recurAverage(a + 1, length -1) * (length - 1)) /length;
}

void oddFirst(int a[], int length)
{
    int i = 0;
    int j = length -1;
    int temp;
    while (i < j)
    {
        while (a[i] % 2 != 0 && i < j)
        {
            i++;
        }
        while (a[j] % 2 == 0 && i < j)
        {
            j--;
        }
        if(i < j)
        {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
            j--;
        }
    }
}

void partition(int a[], int length)
{
    int i = 0;
    int j = length -1;
    int temp;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    temp = a[i];
    while (i < j)
    {
        while (a[j] > temp && i < j)
            j--;
        if(i < j)
        {
            a[i] = a[j];
            ++i;
        }
        while (a[i] < temp && i < j)
            i++;
        if(i < j)
        {

           a[j] = a[i];
           j--;
        }
    }
    a[i] = temp;
}

void printMinAndMax(int a[][maxSize], int n, int m)
{
    int i,j, min,mini, max, maxi, minFlag, maxFlag;
    for(i = 0; i < n; i++)
    {
       min = max = a[i][0];
       mini = maxi = 0;

       for(j = 0; j < m; j++)
       {
           if(a[i][j] < min)
           {
               min = a[i][j];
               mini = j;
           }
           if(a[i][j] > max)
           {
               max = a[i][j];
               maxi = j;
           }
       }
       minFlag = 1;
       for(j = 0; j < n; j++)
       {
           if(j == i)
           {
               continue;
           }
           if(a[j][mini] < min)
           {
               minFlag = 0;
               break;
           }
       }
       if(minFlag)
       {
           std::cout <<"min found " <<  i << "," << mini << ":" << min << std::endl;
       }

        maxFlag = 1;
        for(j = 0; j < n; j++)
        {
            if(j == i)
            {
                continue;
            }
            if(a[j][maxi] > max)
            {
                maxFlag = 0;
                break;
            }
        }
        if(maxFlag)
        {
            std::cout <<"max found " <<  i << "," << maxi << ":" << max << std::endl;
        }
    }
}

void printArr(int a[], int length)
{
    for(int i = 0; i < length; i++)
    {
        std::cout << a[i] << " ";
    }
    std::cout << std::endl;
}

void reset(int op[], int len)
{
    for(int i = 0; i < len; i++)
    {
        op[i] = 0;
    }
}

void mulTriple(int b[][3], int c[][3], int d[][3])
{
    int opnd1[maxSize];
    int opnd2[maxSize];
    int col ,  row, p, q, sum, i;
    d[0][1] = b[0][1];
    d[0][2] = c[0][2];
    int length = 1;
    for(col = 0 ; col < b[0][1]; col++)
    {
        reset(opnd1, b[0][2]);
        for(p = 1; p <= b[0][0]; p++)
        {
            if(b[p][1] == col )
            {
                opnd1[b[p][2]] = b[p][0];
            }
        }
        for(row = 0; row < d[0][2]; row++)
        {
            reset(opnd2, b[0][2]);
            for(q = 1; q <= c[0][0]; q++)
            {
                if(c[q][2] == row)
                {
                    opnd2[c[q][1]] = c[q][0];

                }
            }
            sum = 0;
            for(i = 0; i < b[0][2]; i++)
            {
                sum += opnd1[i] * opnd2[i];
            }
            if(sum != 0)
            {
                d[length][0] = sum;
                d[length][1] = col;
                d[length][2] = row;
                length++;
            }
        }
    }
    d[0][0] = length -1;
}

void addTriple(int b[][3], int c[][3], int d[][3])
{
    d[0][1] = b[0][1];
    d[0][2] = b[0][2];
    int length = 1;
    int p = 1;
    int q = 1;
    while (p <= b[0][0] && q <= c[0][0])
    {
        if(b[p][1] == c[q][1] && b[p][2] == c[q][2])
        {
            d[length][0] = b[p][0] + c[q][0];
            d[length][1] = b[p][1];
            d[length][2] = b[p][2];
            length++;
            p++;
            q++;
        }
        else if(b[p][1] < c[q][1] || (b[p][1] == c[q][1] && b[p][2] < c[q][2]))
        {
            d[length][0] = b[p][0];
            d[length][1] = b[p][1];
            d[length][2] = b[p][2];
            length++;
            p++;
        }
        else
        {
            d[length][0] = c[q][0];
            d[length][1] = c[q][1];
            d[length][2] = c[q][2];
            length++;
            q++;
        }
    }
    while (p <= b[0][0])
    {
        d[length][0] = b[p][0];
        d[length][1] = b[p][1];
        d[length][2] = b[p][2];
        length++;
        p++;
    }
    while (q <= c[0][0])
    {
        d[length][0] = c[q][0];
        d[length][1] = c[q][1];
        d[length][2] = c[q][2];
        length++;
        q++;
    }
    length--;
    d[0][0] = length;

}

void printTriple(int b[][3])
{
    if(b[0][0] == 0)
        return;
    int q = 1;
    int temp;
    for(int i = 0; i < b[0][1]; i++)
    {
        for(int j = 0; j < b[0][2]; j++)
        {
            temp = 0;
            if(b[q][1] == i && b[q][2] == j)
            {
                temp = b[q][0];
                q++;
            }
            std::cout << temp << " ";
        }
        std::cout << std::endl;
    }
}

void transposeTriple(int b[][3], int c[][3])
{
    int col ;
    int p, q;
    c[0][0] = b[0][0];
    c[0][1] = b[0][2];
    c[0][2] = b[0][1];
    if(c[0][0] == 0)
    {
        return;
    }
    q = 1;
    for(col = 0; col < b[0][2];++col)
    {
        for(p = 1; p <= b[0][0]; ++p)
        {
            if(b[p][2] == col)
            {
                c[q][0] = b[p][0];
                c[q][1] = b[p][2];
                c[q][2] = b[p][1];
                q++;
            }
        }
    }
}
int constructTriple(int a[][maxSize], int m, int n,int  b[][3])
{
    int k = 1;
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(a[i][j] != 0)
            {
                b[k][0] = a[i][j];
                b[k][1] = i;
                b[k][2] = j;
                k++;
            }
        }
    }
    b[0][0] = k - 1;
    b[0][1] = m;
    b[0][2] = n;
}



int findInTriple(int b[][3], int x)
{
    int i ;
    if(x == 0)
    {
        if( b[0][1] * b[0][2] > b[0][0])
        {
            return 1;
        }
        return 0;
    }
    for(i = 1; i <= b[0][0]; i++)
    {
        if(b[i][0] == x)
        {
            return 1;
        }
    }
    return 0;
}

#endif //STRUCTURE_ARRAY_H
